package com.baizhi.controller;

import com.baizhi.util.SecurityCode;
import com.baizhi.util.SecurityImage;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.awt.image.BufferedImage;
import java.io.IOException;

@Controller
@RequestMapping("code")
public class CodeController {

    @RequestMapping("code")
    public String getCode(HttpServletRequest request, HttpServletResponse response)throws IOException{
        //设置相应类型
        response.setContentType("image/png");
        //获取验证码
        String securityCode = SecurityCode.getSecurityCode();
        //生成验证码图片
        BufferedImage image = SecurityImage.createImage(securityCode);
        //存入session
        request.getSession().setAttribute("securityCode",securityCode);

        ImageIO.write(image, "png", response.getOutputStream());
        return "";
    }
}
